package com.zgw.his.design.resource.dialog;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.e4.xwt.DefaultLoadingContext;
import org.eclipse.e4.xwt.XWT;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import com.zgw.his.design.common.SWTResourceManager;
import com.zgw.his.design.resource.ResourceTree;

/**
 * 预览病历窗口
 * */
public class PreviewDialog extends Dialog {

	protected Object result;
	protected Shell shell;
	private String path;
	List<Font> listf = new ArrayList<Font>();
	private Composite composite;
	private ScrolledComposite scrolledComposite;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public PreviewDialog(Shell parent, int style, String path) {
		super(parent, style);
		setText("SWT Dialog");
		this.path = path;
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.setMaximized(true);
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	@SuppressWarnings("deprecation")
	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.MAX
				| SWT.APPLICATION_MODAL);
		shell.setSize(495, 572);
		shell.setText("预览");
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));
		{
			scrolledComposite = new ScrolledComposite(shell, SWT.BORDER
					| SWT.H_SCROLL | SWT.V_SCROLL);
			scrolledComposite.setExpandHorizontal(true);
			scrolledComposite.setExpandVertical(true);
			{
				composite = new Composite(scrolledComposite, SWT.NONE);
				composite.setLayout(new GridLayout(1, false));
			}
			scrolledComposite.setContent(composite);
			scrolledComposite.setMinSize(composite.computeSize(SWT.DEFAULT,
					SWT.DEFAULT));
		}
		// 构建XWT控件
		try {
			Display.getDefault().getActiveShell()
					.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_WAIT));
			URL file = new URL("file", "", path);
			XWT.setLoadingContext(new DefaultLoadingContext(ResourceTree.class
					.getClassLoader()));
			Composite com = (Composite) XWT.load(composite, file);
			{
				Rectangle b = com.getBounds();
				GridData gridData = new GridData(SWT.CENTER, SWT.CENTER, true,
						true, 1, 1);
				gridData.widthHint = b.width;
				gridData.heightHint = b.height;
				com.setLayoutData(gridData);
				scrolledComposite.setMinSize(b.width + 5, b.height + 5);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			Display.getDefault().getActiveShell().setCursor(null);
		}
	}
}
